package com.tdcoding.chat.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateHelper {
	private static final String TAG = "DateHelper";

	public static final String ENGLISH_MONTH_FORMAT = "%1$tY-%1$tm";
	public static final String ENGLISH_DAY_FORMAT = "%1$tY-%1$tm-%1$td";
	public static final String ENGLISH_HOUR_FORMAT = "%1$tY-%1$tm-%1$td %1$tH";
	
	public static final String ENGLISH_NOYEAR_FORMAT = "%1$tm-%1$td %1$tH:%1$tM";
	
	public static final String ENGLISH_MINUTE_FORMAT = "%1$tY-%1$tm-%1$td %1$tH:%1$tM";
	public static final String ENGLISH_SECOND_FORMAT = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS";
	
	public static final String CHINA_MONTH_FORMAT = "%1$tY年%1$tm月";
	public static final String CHINA_DAY_FORMAT = "%1$tY年%1$tm月%1$td日";
	
	public static final String CHINA_HOUR_FORMAT = "%1$tY年%1$tm月%1$td日 %1$tH时";
	
	public static final String CHINA_NOYEAR_FORMAT = "%1$tm月%1$td日 %1$tH时%1$tM分";
	public static final String CHINA_MINUTE_FORMAT = "%1$tY年%1$tm月%1$td日 %1$tH时%1$tM分";
	public static final String CHINA_SECOND_FORMAT  = "%1$tY年%1$tm月%1$td日 %1$tH时%1$tM分%1$tS秒";
	
	private static SimpleDateFormat dayFormat=new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat hourFormat=new SimpleDateFormat("yyyy-MM-dd HH");
	private static SimpleDateFormat minuteFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static SimpleDateFormat secondFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 获取到日格式 yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getDayFormat() {
		return dayFormat.format(new Date());
	}

	/**
	 * 获取到小时格式 yyyy-MM-dd HH
	 * 
	 * @return
	 */
	public static String getHourFormat() {
		return hourFormat.format(new Date());
	}

	/**
	 * 获取到分钟格式 yyyy-MM-dd HH:mm
	 * 
	 * @return
	 */
	public static String getMinuteFormat() {
		return minuteFormat.format(new Date());
	}

	/**
	 * 获取到秒格式 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getSecondFormat() {
		return secondFormat.format(new Date());
	}

	/**
	 * 根据提供的格式获取日期或时间
	 * 
	 * @return
	 */
	public static String getNowDate(String format) {
		return String.format(format, new Date());
	}
	
	/**
	 * 根据提供的格式获取日期或时间
	 * 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getFormat(String format,String data) {
		return String.format(format, new Date(secondFormat.format(data)));
	}

	/**
	 * 格式化时间
	 * 
	 * @param time
	 * @return
	 */
	public static String formatDate(String time) {
		Logger.println(TAG, "formatDate", time);
		if (time == null || "".equals(time)) {
			return "";
		}
		Date date = null;
		try {
			date = minuteFormat.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar current = Calendar.getInstance();
		Calendar today = Calendar.getInstance(); // 今天
		today.set(Calendar.YEAR, current.get(Calendar.YEAR));
		today.set(Calendar.MONTH, current.get(Calendar.MONTH));
		today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
		// Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
		today.set(Calendar.HOUR_OF_DAY, 0);
		today.set(Calendar.MINUTE, 0);
		today.set(Calendar.SECOND, 0);

		Calendar yesterday = Calendar.getInstance(); // 昨天

		yesterday.set(Calendar.YEAR, current.get(Calendar.YEAR));
		yesterday.set(Calendar.MONTH, current.get(Calendar.MONTH));
		yesterday.set(Calendar.DAY_OF_MONTH,
				current.get(Calendar.DAY_OF_MONTH) - 1);
		yesterday.set(Calendar.HOUR_OF_DAY, 0);
		yesterday.set(Calendar.MINUTE, 0);
		yesterday.set(Calendar.SECOND, 0);

		current.setTime(date);

		if (current.after(today)) {
			return time.split(" ")[1];
		} else if (current.before(today) && current.after(yesterday)) {
			return "昨天 " + time.split(" ")[1];
		} else {
			int index = time.indexOf("-") + 1;
			return time.substring(index, time.length());
		}
	}

	/**
	 * 判断日期是星期几
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	public static int getWeekByDate(String date) {
		Calendar calendar = Calendar.getInstance();
		try {
			if (date != null) {
				calendar.setTime(dayFormat.parse(date));
			} else {
				calendar.setTime(dayFormat.parse(new Date().toLocaleString()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int week = 0;
		if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
			week = 7;
		} else {
			week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return week;
	}

}
